<template>
  <div id="user-login-page">
<!--    <div class="title"> 鸡哥待办事项管理系统重置密码页面</div>-->
    <a-form
      :model="formState"
      class="reset-password-form"
      @finish="onFinish"
      @finishFailed="onFinishFailed"
    >
      <a-form-item
        :rules="[{ required: true, message: '请输入您的用户名！' }]"
        label="用户名"
        name="username"
      >
        <a-input v-model:value="formState.username">
          <template #prefix>
            <UserOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item
        :rules="[{ required: true, message: '请输入您的旧密码！' }]"
        label="旧密码"
        name="oldPassword"
      >
        <a-input-password v-model:value="formState.oldPassword">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item
        :rules="[{ required: true, message: '请输入您的新密码！' }]"
        label="新密码"
        name="newPassword"
      >
        <a-input-password v-model:value="formState.newPassword">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item
        :rules="[{ required: true, message: '请再次确认新密码！' }]"
        label="确认新密码"
        name="checkPassword"
      >
        <a-input-password v-model:value="formState.checkPassword">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>
      <a-form-item>
        <a-button class="btn-login" html-type="submit" type="primary">
          重置
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { computed, reactive } from 'vue'
import { LockOutlined, UserOutlined } from '@ant-design/icons-vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { useRouter } from 'vue-router'
import { userResetPasswordUsingPost } from '@/api/userController.ts'
import { message } from 'ant-design-vue'

const loginUserStore = useLoginUserStore()
const router = useRouter()

interface FormState {
  username: string;
  oldPassword: string;
  newPassword: string;
  checkPassword: string;
}

const formState = reactive<API.UserResetPasswordRequest>({
  username:'',
  oldPassword: '',
  newPassword: '',
  checkPassword: ''
})
const onFinish = async (values: any) => {
  const res = await userResetPasswordUsingPost({
    ...values
  })
  if (res.data.code === 0 && res.data.data) {
    loginUserStore.loginUser.value = res.data.data
    message.success("操作成功")
    await router.push({
      path: '/user/my'
    })
  }
  else {
    message.error("操作失败：" + res.data.message)
  }
}

const onFinishFailed = (errorInfo: any) => {
  console.log('Failed:', errorInfo)
}
const disabled = computed(() => {
  return !(formState.username && formState.password)
})
</script>
<style scoped>
#user-login-page {
  width: 30%;
  margin-left: 35%;
  margin-top: 8%;

}

.title {
  font-size: 18px;
  color: blueviolet;
  margin-bottom: 3%;
}
</style>
